import 'package:get/get.dart';
import 'package:xindong/manager/xindong_page_singleton.dart';
import 'package:xindong/routes/tab_moments/controller/xindong_dongtai_handle.dart';
import 'package:xindong/routes/tab_moments/model/xindong_dongtai_class.dart';
import 'package:xindong/widgets/dialog/xindong_alert_message.dart';
import 'package:xindong/widgets/dialog/xindong_input_text_send_sheet.dart';
import 'package:xindong/widgets/dialog/xindong_options_sheet.dart';

class XinDongDongTaiActions {
  static const String like = "like"; // 点赞
  static const String comment = "comment"; // 评论
  static const String dashang = "dashang"; // 打赏
  static const String dashan = "dashan"; // 搭讪
}

class XinDongDongTaiActionsCtr extends GetxController {
  static XinDongDongTaiActionsCtr get to => Get.find();

  final _handle = XinDongDongTaiHandle();
  XinDongDongTaiClass? model;

  // 评论临时文本，评论成功后清除
  String? tempCommentText;

  void updateMomentsClass(XinDongDongTaiClass? newClass) {
    if (newClass != null) {
      model = newClass;
    }
  }

  // 请求打赏
  void requestDaShang(XinDongDongTaiClass model) async {
    updateMomentsClass(model);
    _handle.requestDaShang(model).then((value) {
      if (value) {
        int count = int.parse(model.thumbsup) + 1;
        model.thumbsup = "$count";
        model.isthumbsup = 1;
        update([XinDongDongTaiActions.like]);
      }
    });
  }

  // 请求点赞
  void requestPraise(XinDongDongTaiClass model) async {
    updateMomentsClass(model);
    bool enable = model.isthumbsup == 1;
    _handle.requestPraise(model, enable).then((value) {
      if (value) {
        int count = int.parse(model.thumbsup);
        enable ? count-- : count++;
        model.thumbsup = "$count";
        model.isthumbsup = enable ? 0 : 1;
        update([XinDongDongTaiActions.like]);
      }
    });
  }

  // 请求评论
  void requestComment(XinDongDongTaiClass model, {required Function(bool result) sendResult}) async {
    updateMomentsClass(model);

    XinDongInputTextSendSheet.show(
      text: tempCommentText,
      placeholder: "说点什么",
      // 文字修改监听
      textChange: (String text, int cursor) {
        Future.delayed(const Duration(milliseconds: 200)).whenComplete(() {
          tempCommentText = text;
          update();
        });
      },
      // 点击发送
      onTapSend: (text) async {
        bool result = await _handle.requestComment(model, content: tempCommentText);
        if (result) {
          tempCommentText = '';
          int count = int.parse(model.comments ?? "0") + 1;
          model.comments = "$count";
          update([XinDongDongTaiActions.comment]);
          sendResult(true);
        }
      },
    );
    // } else {
    //   AlertOptionsLR.show(
    //       title: '对方已设置免打扰',
    //       content: '由于对方设置，白银以上及会员才可以评论~',
    //       buttonRight: "开通会员",
    //       buttonLeft: "我知道了",
    //       onTabRight: (isCheckbox) {
    //         XinDongJumpManager.instance.jumpMember();
    //       },
    //       onTabLeft: () {});
    // }
  }

  // 更多按钮
  void onTapMore(XinDongDongTaiClass model, {Function(XinDongDongTaiClass model)? onDelete}) async {
    updateMomentsClass(model);

    XinDongOptionsSheet.show(
      dataList: model.isMine ? const ["删除"] : const ["举报"],
      onSelect: (index) {
        if (index == 0) {
          if (model.isMine) {
            XinDongAlertMessage.show(
              content: "是否要删除该条动态？",
              onTapButton: () {
                _handle.requestDelete(model).then((value) {
                  if (value && onDelete != null) {
                    onDelete(model);
                  }
                });
              },
            );
          } else {
            XinDongPageSingleton.instance.jumpToReport(userId: model.uid);
          }

          // XinDongJumpManager.instance.jumpToReportYeMian(
          //   trend: model,
          //   doneCallback: () {
          //     model.isReport = 1;
          //     notifyListeners();
          //   },
          // );
          // } else if () {
          //   bool result = await trendHandle.htmy_requestShield(model);
          //   if (result) {
          //     deleteSuccessCallback(model);
          //     // 详情页要做返回操作
          //     if (isDetails) Navigator.pop(currentContext());
          //   }
        }
      },
    );
  }
}
